<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/7/18
 * Time: 11:46
 */

namespace Biz\model\activity;

use Biz\pockets\constInterface\ActivityInfo;
use Doraemon\pockets\datebase\BaseDbModel;

class FlashSale extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'flash_sale';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    public static function addFlashSale($data)
    {
        if (self::insert($data)->rowcount()) {
            return self::id();
        };
    }

    public static function updateFlashSale($id, $data)
    {
        $data['edittime'] = date('Y-m-d H:i:s');
        if (self::update($data, ['id' => $id])->rowcount()) {
            return $id;
        }
    }

    public static function getFalseSaleIngInfo($time, $field = '*')
    {
        return self::get($field, [
            'end_time[>=]' => $time,
            'status' => self::STATUS_NORMAL,
            'ORDER' => ['start_time' => 'ASC']
        ]);
    }

    public static function getFlashSalePreviewInfos($time, $field = '*')
    {
        return self::get($field, [
            'start_time[>]' => $time,
            'status' => self::STATUS_NORMAL,
            'ORDER' => ['start_time' => 'ASC']
        ]);
    }

    public static function getFlashSaleNoOverInfos($time = null)
    {
        if (!$time) $time = date("Y-m-d H:i:s");
        return self::select("*", [
            'end_time[>]' => $time,
			'end_time[<]' => date( "Y-m-d H:i:s",(floor(time() / 86400) + 2) * 86400) ,
            'status' => self::STATUS_NORMAL,
            'ORDER' => ['start_time' => 'ASC']
        ]);
    }

    public static function getPreviousFlashSale($time = null)
	{
		if (!$time) $time = date("Y-m-d H:i:s");
		return self::get("*", [
			'end_time[<]' => $time,
			'status' => self::STATUS_NORMAL,
			'ORDER' => ['end_time' => 'DESC']
		]);
	}

	// 如果是编辑的话需要过滤自身
    public static function getFiledsByFilter($type = ActivityInfo::ACTIVITY_TYPE_LIMIT_TIME_ROB, $flashSaleId = 0, $fields = '*')
    {
        $where = [];
        $defaultWhere = [
            'type' => $type,
            'end_time[>=]' => date("Y-m-d H:i:s"),
            'status' => self::STATUS_NORMAL
        ];

        if($flashSaleId > 0) {
            $where['id[!]'] = $flashSaleId;
        }
        $where = array_merge($defaultWhere, $where);
        return self::select($fields, $where);
    }

    public static function admSave($data, $adminuid) {
    	$currentTime = date("Y-m-d H:i:s");
    	if (isset($data['flash_id']) && $data['flash_id']) {
    		$id = $data['flash_id'];
    		self::update([
    			'start_time' => $data['start_time'],
				'end_time' => $data['end_time'],
				'edittime' => $currentTime,
				'admin_id' => $adminuid,
			], ['id' => $id]);
		} else {
    		self::insert([
				'start_time' => $data['start_time'],
				'end_time' => $data['end_time'],
				'addtime' => $currentTime,
				'admin_id' => $adminuid,
			]);
    		$id = self::id();
		}
		return $id;
	}
}